package com.microframework.boot.system.controller;

import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.microframework.base.core.model.options.TreeNode;
import com.microframework.base.core.util.SecurityUtils;
import com.microframework.base.core.util.UuidUtil;
import com.microframework.base.web.page.PageQuery;
import com.microframework.base.web.resp.R;
import com.microframework.boot.system.model.domain.Menu;
import com.microframework.boot.system.service.IMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

/**
 * 菜单管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/sys/menu")
@Tag(name = "菜单管理", description = "菜单管理相关接口")
public class MenuController {

    @Autowired
    private IMenuService menuServiceService;

    @PostMapping("/load/all")
    @Parameters({ @Parameter(name = "query", description = "查询条件", required = false) })
    @Operation(summary = "一次性加载树型数据", description = "一次性加载树型数据")
    public R<List<Menu>> loadAll(@RequestBody PageQuery<Menu> query) {
        return R.ok(menuServiceService.loadAll(query));
    }
    
    @PostMapping("/load/lazy")
    @Operation(summary = "懒加载树型数据", description = "懒加载树型数据")
    public R<List<Menu>> loadLazy(@RequestBody PageQuery<Menu> pageQuery) {
        return R.ok(menuServiceService.loadLazy(pageQuery));
    }
    
    @PostMapping("/select/tree")
    @Operation(summary = "树形下拉框", description = "树形下拉框数据")
    public R<List<TreeNode<Menu>>> tree(@RequestBody PageQuery<Menu> pageQuery) {
        return R.ok(menuServiceService.tree(pageQuery));
    }
    
	@Operation(summary = "保存菜单")
	@Parameters({ @Parameter(name = "menu", description = "菜单", required = true) })
	@PostMapping("/save")
	public R<Menu> save(HttpServletRequest request, @RequestBody Menu menu) {
		try {
			if (StrKit.notBlank(menu.getId())) {
				Menu checkMenu=Menu.dao.findFirst("select id from "+Menu.TABLE_NAME+" where code=? and id!=?",menu.getCode(),menu.getId());
				if(checkMenu!=null) {
					return R.fail("标识重复.");
				}
				menu.setModifyTime(new Date(System.currentTimeMillis()));
				menu.setModifyUserId(SecurityUtils.getUserId());
				if (menu.update()) {
					return R.ok(menu);
				} else {
					return R.fail("更新失败.");
				}
			} else {
				Menu checkMenu=Menu.dao.findFirst("select id from "+Menu.TABLE_NAME+" where code=? ",menu.getCode());
				if(checkMenu!=null) {
					return R.fail("标识重复.");
				}
				menu.setId(UuidUtil.getUUID());
				menu.setCreateTime(new Date(System.currentTimeMillis()));
				menu.setCreateUserId(SecurityUtils.getUserId());
				if (menu.save()) {
					return R.ok(menu);
				} else {
					return R.fail("保存失败.");
				}
			}
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}
	
	@Operation(summary = "删除菜单")
	@Parameters({ @Parameter(name = "id", description = "菜单id", required = true) })
	@DeleteMapping("/detain/{id}")
	public R<String> deleteById(@PathVariable("id") String id) {
		try {
			Menu menu=new Menu();
			menu.setId(id);
			menu.setIsDel("1");
			if(menu.update()) {
				return R.ok("删除成功.");
			}else {
				return R.fail("删除失败.");
			}
		}catch (Exception e) {
			log.error("删除异常:{}",e.getMessage());
			return R.fail(e.getMessage());
		}
	}
} 